/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.saar.penguin.give2.server.alert;

import de.saar.penguin.give2.server.BigBrother;
import de.saar.penguin.give2.server.DescribedObject;
import de.saar.penguin.give2.server.Instruction;
import de.saar.penguin.give2.server.REgen;
import de.saar.penguin.give2.server.SaarNlgSystem;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author koller
 */
public class ReHasChangedAlert extends Alert {
    private REgen regen;
    private Map<String, DescribedObject> describedObjects;
    private final String rexp;
    private final DescribedObject targetObject;
    private Set<String> visibleObjectsForRexp;

    public ReHasChangedAlert(SaarNlgSystem nlg, String rexp, DescribedObject targetObject, REgen regen, Map<String, DescribedObject> describedObjects) {
        super(nlg);

        this.rexp = rexp; //an RE created for targetObject
        this.regen = regen; //the RE generator, will be used to create an RE that is compared to the original one
        this.targetObject = targetObject; //the Object we are currently referring to
        this.describedObjects = describedObjects; //required by REgen to find distractors
        this.visibleObjectsForRexp = new HashSet<String>(nlg.visible);
    }

    @Override
    public boolean isActivated(BigBrother bb) {
        if (nlg.isSendUpdatedREs()) {
            Set<String> currentlyVisible = new HashSet<String>(nlg.visible);
            if (currentlyVisible.equals(visibleObjectsForRexp)) {
                return false;
            } else {
                String s = regen.olgarithm(describedObjects, targetObject, nlg.visible);
                visibleObjectsForRexp = currentlyVisible;
                return !rexp.equals(s);
            }
        } else {
            return false;
        }
    }

    @Override
    public Instruction getInstruction() {
        return nlg.instructionGenerator.createInstruction(nlg.visible);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ReHasChangedAlert other = (ReHasChangedAlert) obj;
        if (this.describedObjects != other.describedObjects && (this.describedObjects == null || !this.describedObjects.equals(other.describedObjects))) {
            return false;
        }
        if ((this.rexp == null) ? (other.rexp != null) : !this.rexp.equals(other.rexp)) {
            return false;
        }
        if (this.targetObject != other.targetObject && (this.targetObject == null || !this.targetObject.equals(other.targetObject))) {
            return false;
        }
        if (this.visibleObjectsForRexp != other.visibleObjectsForRexp && (this.visibleObjectsForRexp == null || !this.visibleObjectsForRexp.equals(other.visibleObjectsForRexp))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + (this.describedObjects != null ? this.describedObjects.hashCode() : 0);
        hash = 59 * hash + (this.rexp != null ? this.rexp.hashCode() : 0);
        hash = 59 * hash + (this.targetObject != null ? this.targetObject.hashCode() : 0);
        hash = 59 * hash + (this.visibleObjectsForRexp != null ? this.visibleObjectsForRexp.hashCode() : 0);
        return hash;
    }

    
}
